/*************************************************************************
	> File Name: 225.用队列实现栈_v2.c
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 二  9/ 7 18:40:37 2021
 ************************************************************************/

typedef struct {
    int *data;
    int head;
    int tail;
    int length;
    int capacity;
} MyStack;

/** Initialize your data structure here. */

MyStack* myStackCreate() {
    MyStack *sta = (MyStack *)malloc(sizeof(MyStack));
    sta->data = (int *)malloc(sizeof(int) * 100);
    sta->head = 0;
    sta->tail = 0;
    sta->length = 0;
    sta->capacity = 100;
    return sta;
}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
    //if (obj->tail == obj->capacity) obj->tail = 0;
    obj->data[obj->tail++] = x;
    int n = obj->length;
    while (n--) {
        obj->data[obj->tail++] = obj->data[obj->head];
        obj->head++;
    }
    obj->length++;
}

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
    if (obj->length == 0) return -1;
    int data = obj->data[obj->head++];
    obj->length--;
    return data;
}

/** Get the top element. */
int myStackTop(MyStack* obj) {
    return obj->data[obj->head];
}

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
    return obj->length == 0;
}

void myStackFree(MyStack* obj) {
    if (obj == NULL) return ;
    free(obj->data);
    free(obj);
    return ;
}

/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);
 
 * int param_2 = myStackPop(obj);
 
 * int param_3 = myStackTop(obj);
 
 * bool param_4 = myStackEmpty(obj);
 
 * myStackFree(obj);
*/
